/**
                                                                                                                                  * Created by lanyanhua on 2018/10/31 0031.
 */
NProgress.start();
window.onload = function () {
    NProgress.done();
};
layui.use(['element', 'table', 'form', 'jquery', 'laydate','list'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var list = layui.list;
    var element = layui.element;
    //数据表格
    var showdatatable = table.render({
        elem: '#tableId',
        id:'showdata',
        url: '/goods/findGoodsPage',
        limit: 10,
        page: true,
        while: param(),
        cols: [ [
            {type: 'checkbox'},
            {field: 'goodsId', title: 'ID', align: 'center', sort: true ,width: 60},
            {field: 'goodsName', title: '物品名称', align: 'center',width: 190},
            {field: 'goodsCode', title: '物品编号', align: 'center',width: 90},
            {field: 'roleId', title: '所属角色', align: 'center',width: 120},
            {field: 'status', title: '状态',templet: '#statusTpl', align: 'center',width: 80},
            {field: 'location', title: '所在位置', align: 'center',width: 350},
            {field: 'remark', title: '位置描述', align: 'center',width: 320},
            {title: '操作', templet: '#operationTpl',width: 120, align: 'center',fixed:'right'}
        ] ],
        done: function (res, curr, count) {
            $("#countNum").text(count);
        }
    });
    //list.open(options); 表单配置
    var listOpen  = {
        elem:"#groupAdd",
        fields:{
        	goodsId:{label:'ID',type:'hidden'},
        	goodsName:{label: '物品名称'},
        	goodsCode:{label: '物品编号'},
        	roleId : {label:"所属角色"},
        	status : {label:"物品状态"},
        	location : {label:"所在位置"},
        	remark : {label:"备注1"},
        	remark2:{label:"备注2"},
        	remark3:{label:"备注3"}
        },
        submit : function(res){ //表单提交事件
            $.ajax({
                type : 'post',
                url : '/goods/saveGoods',
                data : res.field ,
                dataType : 'json',
                success : function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index); //再执行关闭
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                }
            });
            return false;
        },
        btns:["submit",'reset'],
        area:['70%','95%']
    };
    // 提交时间
    form.on('submit(search)', function () {
        showdatatable.reload({where: param()});
        return false;
    });
    
   // 修改 状态
    form.on('switch(status)', function (obj) {
        var $this = this;
        layer.confirm("确定要修改当前状态吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function (index) {
            $.post('/goods/updateStatus', {goodsId: $this.value, status: obj.elem.checked ? 1 : 0}, function (data) {
                layer.close(index);
                if (data.success) {
                    layer.msg(data.message, {icon: 1, time: 1000});
                   // layui.table.reload('showdata', {page: {curr: 1}});
                } else {
                    layer.msg(data.message, {icon: 2, time: 5000});
                }
            })
        });
    });
    
    // 编辑按钮 ， 删除按钮
    table.on('tool(tableFilter)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            //console.log("编辑");
            listOpen.over = data;
            list.open(listOpen);
        } else if (layEvent === 'del') {
            layer.confirm("确定要执行吗？", {skin: 'layui-layer-lan', icon: 2, title: '提示', anim: 6}, function () {
                $.post('/goods/deleteGoods',{goodsId:data.goodsId},function (data) {
                    if(data.success){
                        layer.msg(data.message, {icon: 1, time: 1000});
                        layui.table.reload('showdata',{page:{curr:1}});
                    }else{
                        layer.msg(data.message, {icon: 2, time: 5000});
                    }
                })
            });
        }
    });
    // 添加时间
    $("#addGroup").click(function(){
        listOpen.over = null;
        //弹出框 
        list.open(listOpen);
    });
    
    
    function  param() {
        return {
        	goodsName: $('[name=goodsName]').val(),
        	roleId: $('[name=roleId]').val(),
        	status: $('[name=status]').val(),
        	remark: $('[name=remark]').val(),
        }
    }
});
